<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>骏怡连锁酒店后台管理-登录</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <!-- Bootstrap Styles-->
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
    <!-- FontAwesome Styles-->
    <link href="assets/css/font-awesome.css" rel="stylesheet" />
    <!-- Sweet Alert-->
    <link rel="stylesheet" href="assets/css/sweetalert2.min.css">
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
        }

        body {
            background: url(1.jpg)no-repeat;
            background-size: cover;
            overflow: hidden;
        }

        .whole {
            width: 100%;
            height: 100%;
        }

        .left {
            width: 50%;
            float: left;
        }

        .right {
            width: 50%;
            ;
            float: left;
        }

        .content {
            width: 450px;
            height: 300px;
            background: transparent;
            margin: 200px auto;
            margin-left: 0;
            text-align: center;
            color: white;
            border-left: 2px solid rgb(31, 40, 55);
        }

        input {
            margin-top: 30px;
        }

        button {
            outline: medium;
            margin-top: 30px;
            width: 170px;
            border: 1px solid rgb(244, 67, 54);
            border-radius: 50px;
            background: rgb(31, 40, 55);
            color: white
        }

        .a input {
            border: none;
            outline: medium;
            background: transparent;
            border-bottom: 1px solid rgb(31, 40, 55);
            color: #fff
        }

        button:hover {
            cursor: pointer;
        }

        .center {
            width: 450px;
            height: 100px;
            text-align: center;
            color: white;
            font-size: 30px;
            margin: 300px auto;
            margin-right: 0;

        }
        #btnzmx{
         margin-bottom: 20px;
        float: right;
     }
    </style>
</head>

<body>
    <div class='whole'>
        <div class='left'>
            <div class='center'>
                <img src="favicon.ico" />
                <br>骏怡连锁酒店后台管理系统</div>
        </div>
        <div class='right'>
            <div class='content'>
                <h2>用户登录</h2>
                <div class='a'>
                    <i class="fa fa-user"></i>
                    <input type='username' id='username'  placeholder="请输入用户名或手机号" autocomplete="off" style="border-radius:3px;">
                </div>
                <div class='a'>
                    <i class="fa fa-lock"></i>
                    <input type='password' id='password' placeholder="请输入密码" autocomplete="off" style="border-radius:3px;">
                </div>
                <div style="margin-top: 10px;">
                    <label>
                            <input id='remember' type='checkbox'>记住密码
                            <button id="findPass" class="btn btn-link" style="color:#fff;margin:0;width: 80px;">忘记密码</button>
                    </label>
                </div>
                <button id='btn' class="btn btn-info btn-sm">登录</button>
            </div>
        </div>
    </div>
</body>
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- Sweet Alert -->
<script src="assets/js/sweetalert2.min.js"></script>
<script>
    var username = $('#username')
    var password = $('#password')
    var remember = $('#remember')
    var btn = $('#btn')
    //登录
    btn.click(validate);
    password.keyup(function(e){
        e.keyCode==13 && validate()
    })
    function validate() {
        if (username.val().trim() === '' || password.val().trim() === '') {
            swal({
                type: 'error',
                title: '登录失败!',
                text: '用户名或密码不能为空'});
            return;
        }
        $.ajax({
            url:'login',
            method:'post',
            data:`username=${username.val().trim()}&password=${password.val().trim()}`,
            success:function(data){
                if(data === 'success'){
                    if (remember.checked) {
                        localStorage.setItem('username', username.val().trim())
                    } else {
                        sessionStorage.setItem('username', username.val().trim())
                    }
                    location.assign('index.html');
                }else{
                    swal({
                        type: 'error',
                        title: '登录失败!',
                        text: '用户名或密码错误'});
                }
            }
        })
    }

    //找回密码
    $("#findPass").click(function(){
        swal({
                title: '重置密码',
                html:`<input id="swal-phone" class="swal2-input" placeholder="请输入手机号码">
                        <input id="swal-code" class="swal2-input" placeholder="请输入验证码" style="width:60%;">&nbsp;
                        <button class="btn btn-default btn-lg" id="sendCode" style="margin-top:0;padding:9px 16px;">发送验证码</button>
                        <input id="swal-pass" class="swal2-input" placeholder="请输入新密码">`,
                showCancelButton: true,
                preConfirm: function (value) {
                    let phone=$('#swal-phone').val();
                    let code=$('#swal-code').val();
                    let password=$('#swal-pass').val();
                    if(!phone || !code || !password){
                        return;
                    }
                    // 验证
                    $.ajax({
                        url: 'validateSMS',
                        method: "POST",
                        data: `phone=${phone}&code=${code}&password=${password}`,
                        success: function (data) {
                            if (data == 'success') {
                                swal('修改成功','密码重置成功,可立即登录','success');
                            }else{
                                swal('修改失败','验证码有误','error');
                            }
                        }
                    })
                }
            })
    })

    /**
     * 发送短信验证码
     * */
    $(document).on('click',"#sendCode",function(){
        let phone = $("#swal-phone").val();
        if(!phone) return;
        $.ajax({
            url:'sendSMS',
            method:'POST',
            data:`phone=${phone}`,
            success:function(result){
                $('#sendCode').prop('disabled',true);
                timer($('#sendCode'));
            }
        })
    })

    function timer(jqElement){
        let i=60;
        let timer=setInterval(function(){
            jqElement.html(i);
            i--;
            if(i<=0){
                clearInterval(timer);
                jqElement.prop('disabled',false).html('发送短信');
            }
        },1000);
    }

</script>

</html>